import DestructibleFacility from "../facility/DestructibleFacility";
import HealthPoint, { Killer } from "../funcObj/interface/HealthPoint";
import { Location } from "../info/Predefine";
import GameManager from "../manager/GameManager";
import NodePoolManager from "../manager/NodePoolManager";
import Player from "../player/Player";
import Flyer from "./Flyer";

const { ccclass, property } = cc._decorator;

/**
 * 可被摧毁的飞行物
 */
@ccclass
export default abstract class DestructibleFlyer extends Flyer implements HealthPoint {

    @property(cc.ProgressBar)
    hpProgress: cc.ProgressBar = null;

    @property(cc.Collider)
    collider: cc.Collider = null;

    maxHp: number;

    currentHp: number;

    init(base: DestructibleFacility, target?: HealthPoint | cc.Vec3 | Location): void {
        super.init(base, target);
        this.initHp();
        this.collider.enabled = false;
    }

    public dispatch(target: HealthPoint | cc.Vec3 | Location) {
        this.collider.enabled = true;
    }

    /**
     * 发射成功通知基地继续发射
     * @param time 
     */
    protected dispatchSuccess(time: number = 1) {
        if (time) {
            this.scheduleOnce(() => {
                this.node.emit(DestructibleFacility.EventType.发射成功);
            }, time);
        } else {
            this.node.emit(DestructibleFacility.EventType.发射成功);
        }
    }

    /**
     * 初始化血量
     */
    initHp() {
        this.hpProgress.node.children[0].getComponent(cc.Sprite).spriteFrame = this.player.flyerhpColor;
        this.maxHp = this.flyerInfo.生命值;
        this.currentHp = this.maxHp;
        this.hpProgress.node.active = false;
        this.hpProgress.progress = 1;
    }

    /**
     * 恢复血量
     * @param hp 
     */
    repairHp(hp: number): void {
        this.currentHp = Math.min(this.maxHp, this.currentHp + hp);
        this.hpProgress.node.active = this.currentHp < this.maxHp;
        this.hpProgress.progress = this.currentHp / this.maxHp;
    }

    /**
     * 失去血量
     * @param killer 
     * @returns 
     */
    reducedHp(killer: Killer): boolean {
        killer.domage = Math.max(1, killer.domage);
        this.currentHp = Math.max(0, this.currentHp - killer.domage);
        this.hpProgress.node.active = true;
        this.hpProgress.progress = this.currentHp / this.maxHp;
        this.player.node.emit(Player.EventType.我的飞行物被攻击, this, killer);
        if (this.currentHp == 0) {
            this.die(killer);
            return true;
        } else {
            return false;
        }
    }

    /**
     * killer 抵达后爆炸的 为空
     * @param killer 
     */
    die(killer: Killer) {
        //通知 攻击目标为我的近防炮子弹
        this.node.emit(Flyer.EventType.摧毁, this);
        this.node.off(Flyer.EventType.摧毁);
        this.collider.enabled = false;
        // 回收
        NodePoolManager.instance.recycle(this.node);
        // 播放爆炸动画
        let size = this.node.getContentSize();
        let big = Math.max(size.height, size.width) * 2;
        size = cc.size(big * this.node.scale, big * this.node.scale);
        GameManager.instance.addFlyerExploadEffect(this.flyerType, this.node.position, size);
        super.onArrive();
    }

    onArrive() {
        this.die(null);
    }

}